﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace IssueTrackerMvcWeb.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private HomeControllerViewData viewData = new HomeControllerViewData();

        public ActionResult Index()
        {
            GetTaskList();

            return View(viewData);
        }

        public ActionResult About()
        {
            ViewData["Title"] = "About Page";

            return View();
        }

        public ActionResult Add()
        {
            //Get Project list
            IssueTrackerService.IssueTrackerServiceClient client = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                client.Open();

                viewData.ProjectList = client.ListProjects().ToList<IssueTrackerService.Project>();

                viewData.NewIssue = new IssueProcessService.Issue();
            }
            finally
            {
                client.Close();
            }

            return View(viewData);
        }

        public ActionResult Edit(System.Guid id)
        {
            //Get Issue
            IssueTrackerService.IssueTrackerServiceClient client = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                client.Open();

                viewData.Issue = client.GetIssue(id);

                viewData.ProjectList = client.ListProjects().ToList<IssueTrackerService.Project>();

                viewData.Actions = ConstructWorkflowActionList(client.GetEventList(viewData.Issue.CurrentStateName));
                
            }
            finally
            {
                client.Close();
            }

            return View(viewData);
        }

        private Dictionary<string, string> ConstructWorkflowActionList(Dictionary<string, string> actions)
        {
            var returnDict = new Dictionary<string, string>();
            returnDict.Add("", "");

            foreach (KeyValuePair<string, string> p in actions)
            {
                returnDict.Add(p.Key, p.Value);
            }
            return returnDict;
        }

        public ActionResult Delete(System.Guid id)
        {
            IssueTrackerService.IssueTrackerServiceClient client = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                client.Open();

                viewData.Issue = client.GetIssue(id);

                BindingHelperExtensions.UpdateFrom(viewData.Issue, Request.Form);

                client.DeleteIssue(viewData.Issue);

                viewData.Message = "Issue deleted.";
            }
            catch (Exception ex)
            {
                viewData.Message = ex.Message;
            }
            finally
            {
                if(client.State== System.ServiceModel.CommunicationState.Opened)
                    client.Close();

                
            }

            GetTaskList();

            return View("Index", viewData);
        }

        public ActionResult Create()
        {
            IssueProcessService.IssueProcessClient processClient = new IssueProcessService.IssueProcessClient();

            try
            {
                viewData.NewIssue = new IssueProcessService.Issue();
                BindingHelperExtensions.UpdateFrom(viewData.NewIssue, Request.Form);
                viewData.NewIssue.Id = Guid.NewGuid();

                processClient.ProcessIssue(viewData.NewIssue);

                viewData.Message = "Issue created.";
            }
            catch (Exception ex)
            {
                viewData.Message = ex.Message;
            }
            finally
            {
                if (processClient.State == System.ServiceModel.CommunicationState.Opened)
                    processClient.Close();
            }

            GetTaskList();
            
            return View("Index", viewData);
        }

        public ActionResult Update(Guid id)
        {
            IssueTrackerService.IssueTrackerServiceClient updateClient = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                viewData.Issue = updateClient.GetIssue(id);

                BindingHelperExtensions.UpdateFrom(viewData.Issue, Request.Form);

                updateClient.UpdateIssue(viewData.Issue);

                viewData.Message = "Issue updated.";

                if (Request.Form["Action"] != null)
                {
                    viewData.Message = viewData.Message + "<br/> Workflow Action - " +Request.Form["Action"]+ " triggered.";
                }
            }
            catch (Exception ex)
            {
                viewData.Message = ex.Message;
            }
            finally
            {
                if (updateClient.State == System.ServiceModel.CommunicationState.Opened)
                    updateClient.Close();               
            }

            GetTaskList();

            return View("Index", viewData);
        }

        #region for Tech.Ed ILL
        public ActionResult ValidateIssue(Guid id)
        {
            IssueTrackerService.IssueTrackerServiceClient updateClient = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                viewData.Issue = updateClient.GetIssue(id);

                BindingHelperExtensions.UpdateFrom(viewData.Issue, Request.Form);

                viewData.Issue.CurrentStateName = "Pending Solution";

                updateClient.UpdateIssue(viewData.Issue);

                viewData.Message = "Issue updated.";

            }
            catch (Exception ex)
            {
                viewData.Message = ex.Message;
            }
            finally
            {
                if (updateClient.State == System.ServiceModel.CommunicationState.Opened)
                    updateClient.Close();
            }

            GetTaskList();

            return View("Index", viewData);
        }

        public ActionResult RejectIssue(Guid id)
        {
            IssueTrackerService.IssueTrackerServiceClient updateClient = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                viewData.Issue = updateClient.GetIssue(id);

                BindingHelperExtensions.UpdateFrom(viewData.Issue, Request.Form);

                viewData.Issue.CurrentStateName = "Issue Closed";

                updateClient.UpdateIssue(viewData.Issue);

                viewData.Message = "Issue updated.";

            }
            catch (Exception ex)
            {
                viewData.Message = ex.Message;
            }
            finally
            {
                if (updateClient.State == System.ServiceModel.CommunicationState.Opened)
                    updateClient.Close();
            }

            GetTaskList();

            return View("Index", viewData);
        }

        public ActionResult SolutionCompleted(Guid id)
        {
            IssueTrackerService.IssueTrackerServiceClient updateClient = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                viewData.Issue = updateClient.GetIssue(id);

                BindingHelperExtensions.UpdateFrom(viewData.Issue, Request.Form);

                viewData.Issue.CurrentStateName = "Pending Testing";

                updateClient.UpdateIssue(viewData.Issue);

                viewData.Message = "Issue updated.";

            }
            catch (Exception ex)
            {
                viewData.Message = ex.Message;
            }
            finally
            {
                if (updateClient.State == System.ServiceModel.CommunicationState.Opened)
                    updateClient.Close();
            }

            GetTaskList();

            return View("Index", viewData);
        }

        public ActionResult SolutionAccepted(Guid id)
        {
            IssueTrackerService.IssueTrackerServiceClient updateClient = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                viewData.Issue = updateClient.GetIssue(id);

                BindingHelperExtensions.UpdateFrom(viewData.Issue, Request.Form);

                viewData.Issue.CurrentStateName = "Issue Closed";

                updateClient.UpdateIssue(viewData.Issue);

                viewData.Message = "Issue updated.";

            }
            catch (Exception ex)
            {
                viewData.Message = ex.Message;
            }
            finally
            {
                if (updateClient.State == System.ServiceModel.CommunicationState.Opened)
                    updateClient.Close();
            }

            GetTaskList();

            return View("Index", viewData);
        }

        public ActionResult SolutionRejected(Guid id)
        {
            IssueTrackerService.IssueTrackerServiceClient updateClient = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                viewData.Issue = updateClient.GetIssue(id);

                BindingHelperExtensions.UpdateFrom(viewData.Issue, Request.Form);

                viewData.Issue.CurrentStateName = "Pending Solution";

                updateClient.UpdateIssue(viewData.Issue);

                viewData.Message = "Issue updated.";

            }
            catch (Exception ex)
            {
                viewData.Message = ex.Message;
            }
            finally
            {
                if (updateClient.State == System.ServiceModel.CommunicationState.Opened)
                    updateClient.Close();
            }

            GetTaskList();

            return View("Index", viewData);
        }

        #endregion

        [NonAction]
        private void GetTaskList()
        {
            //Get Task List
            IssueTrackerService.IssueTrackerServiceClient client = new IssueTrackerMvcWeb.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                client.Open();

                viewData.TaskList = client.TaskList("", "").ToList<IssueTrackerService.Issue>();
            }
            finally
            {
                client.Close();
            }
        }

    }

    public class HomeControllerViewData
    {
        public List<IssueTrackerService.Issue> TaskList {get; set;}
        public string Message { get; set; }
        public IssueTrackerService.Issue Issue { get; set; }
        public List<IssueTrackerService.Project> ProjectList { get; set; }
        public IssueProcessService.Issue NewIssue { get; set; }
        public Dictionary<string, string> Actions { get; set; }
    }

    
}
